package com.block.datapush.special.api;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.service.LogFactory;
import com.block.datapush.model.result.ApiPushResult;
import com.block.datapush.special.enums.OrderStatusEnum;
import com.block.db.entity.MemberExtInfo;
import com.block.db.entity.SpecialCostomInfo;
import com.block.db.entity.SpecialDataApiInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * api 导量
 *
 */
@Service
public class CommonApiPushService implements IApiPushService {

    private static final Logger log = LogFactory.getPushLog();

    @Override
    public ApiPushResult matchUser(String orderNo, SpecialCostomInfo costomInfo, MemberExtInfo memberExtInfo, JSONObject jsonObject) throws Exception {

        String queryUrl = costomInfo.getApiMatchUrl();
        String mobile = SecureUtil.md5(memberExtInfo.getMobile()).toLowerCase();

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("mobile", mobile);

        log.info("【贷超撞库】撞库开始【queryUrl】{}【paramMap】{}", queryUrl, JSON.toJSONString(paramMap));
        String result = HttpUtil.createPost(queryUrl)
                .body(JSON.toJSONString(paramMap))
                .execute().body();
        log.info("【贷超撞库】撞库结束【result】{}", result);

        JSONObject resultObject = JSON.parseObject(result);
        String code = resultObject.getString("code");
        String message = resultObject.getString("message");

        ApiPushResult apiPushResult = new ApiPushResult();
        apiPushResult.setSuccess(false);
        apiPushResult.setBizParam(JSON.toJSONString(paramMap));
        apiPushResult.setResult(result);
        apiPushResult.setCode(code);
        apiPushResult.setMessage(message);

        if (Objects.equals(code, "200")) {
            apiPushResult.setSuccess(true);
        }
        return apiPushResult;

    }

    @Override
    public ApiPushResult register(String orderNo, SpecialCostomInfo costomInfo, MemberExtInfo memberExtInfo, JSONObject jsonObject) throws Exception {

        ApiPushResult apiPushResult = new ApiPushResult();
        apiPushResult.setOrderNo(orderNo);

        String requestUrl = costomInfo.getApiRegisterUrl();

        Map<String, Object> bizMap = new HashMap<>();
        bizMap.put("file_id",costomInfo.getApiCode());
        bizMap.put("promotion",costomInfo.getApiKey());
        bizMap.put("name",memberExtInfo.getUsername());
        bizMap.put("mobile",memberExtInfo.getMobile());
        bizMap.put("age",memberExtInfo.getAge());
        bizMap.put("sex",memberExtInfo.getSex());
        bizMap.put("city", memberExtInfo.getCity());
        bizMap.put("is_company",1);
        bizMap.put("is_credit","0");
        bizMap.put("is_work",1);
        bizMap.put("is_tax",1);
        bizMap.put("webank",0);

        log.info("【贷超进件】导单开始【requestUrl】{}【paramMap】{}", requestUrl, JSON.toJSONString(bizMap));
        String result = HttpUtil.createPost(requestUrl)
                .body(JSON.toJSONString(bizMap))
                .execute().body();
        log.info("【贷超进件】导单结束【result】{}", result);

        JSONObject resultObject = JSON.parseObject(result);
        String code = resultObject.getString("code");
        String message = resultObject.getString("message");

        apiPushResult.setResult(JSON.toJSONString(resultObject));
        apiPushResult.setCode(String.valueOf(code));
        apiPushResult.setMessage(message);
        apiPushResult.setBizParam(JSON.toJSONString(bizMap));

        if (Objects.equals(code, "200")) {
            apiPushResult.setSuccess(true);
            apiPushResult.setSettleFlag(true);
            apiPushResult.setStatus(OrderStatusEnum.SUCCESS.getValue());
            return apiPushResult;
        }

        apiPushResult.setSuccess(false);
        apiPushResult.setSettleFlag(false);
        apiPushResult.setStatus(OrderStatusEnum.FAILED.getValue());
        return apiPushResult;
    }

    @Override
    public ApiPushResult queryResult(SpecialCostomInfo costomInfo, SpecialDataApiInfo dataApiInfo, JSONObject jsonObject) {
        return null;
    }
}
